package com.ewei.web.common.json.resolver;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.ewei.web.common.exception.MessageRuntimeException;
import com.ewei.web.common.json.JsonUtils;

//对Map的封装
public class JsonModelMap {
	private static final Logger logger = LogManager.getLogger(JsonModelMap.class);
	private Map<String,Object> map = new HashMap<String,Object>();
	public void merge(Map<String,Object> otherMap){
		Set<Entry<String,Object>> entrySet = otherMap.entrySet();
		for(Entry<String,Object> entry:entrySet){
			map.put(entry.getKey(),entry.getValue());
		}
	}
	public Long getLong(String key){
		Object obj = map.get(key);
		if(null!=obj){
			return new Long(obj.toString());
		}
		return null;
	}
	public Integer getInteger(String key){
		Object obj = map.get(key);
		if(null!=obj){
			return new Integer(obj.toString());
		}
		return null;
	}
	public Double getDouble(String key){
		Object obj = map.get(key);
		if(null!=obj){
			return new Double(obj.toString());
		}
		return null;
	}
	public Long getDate(String key){//将日期转化为Long数值输出
		Object obj = map.get(key);
		if(null!=obj){
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			try {
				return format.parse(obj.toString()).getTime();
			} catch (ParseException e) {
				logger.info(e);
				throw new MessageRuntimeException(e);
			}
		}
		return null;
	}
	public <T> T getObject(String key,Class<?> clz){
		Object obj = map.get(key);
		if(null!=obj){
			String json = map.toString();
			return JsonUtils.fromJson(json,clz);
		}
		return null;
	}
	public Map<String, Object> getMap() {
		return map;
	}
	public void setMap(Map<String, Object> map) {
		this.map = map;
	}
	
}
